#include "StdAfx.h"
#include "MyListBox.h"


MyListBox::MyListBox(void)
{
}


MyListBox::~MyListBox(void)
{
}



HWND MyListBox::createListBox(int styles,int x, int y, int cx,int cy, HWND hParent, int id,
	int nHeight,int fnWeight , BOOL fdwItalic , BOOL fdwUnderline, LPTSTR font, HINSTANCE hInst)
{
	InitCommonControls();

	hInstance = hInst;
	hListBox = CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("LISTBOX"), NULL, styles, x, y, cx, cy, hParent, (HMENU) id, hInst, NULL); 	

	HFONT hFont = CreateFont(nHeight,0,0,0,fnWeight,fdwItalic,fdwUnderline,0,VIETNAMESE_CHARSET,OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, VARIABLE_PITCH,font);

	SendMessage(hListBox,WM_SETFONT,(WPARAM)hFont, 0);

	return hListBox;
}

void MyListBox::hide()
{
	ShowWindow(hListBox,SW_HIDE);
}


void MyListBox::show()
{
	ShowWindow(hListBox,SW_SHOW);
}


void MyListBox::move(int x, int y, int cx, int cy)
{
	MoveWindow(hListBox,x, y, cx, cy,TRUE);
}
void MyListBox::setHCroll(int maxWidth)
{
	SendMessage(hListBox, LB_SETHORIZONTALEXTENT,maxWidth,0);
}


HWND MyListBox::getHandle()
{
	return hListBox;
}

int MyListBox::count()
{
	return SendMessage(hListBox,LB_GETCOUNT,0,0);
}
void MyListBox::addItem(LPTSTR itemAdd)
{
	SendMessage(hListBox,LB_ADDSTRING,0,(LPARAM)itemAdd);
}

void MyListBox::deleteItem()
{
	int i = getSelected();
	if(i >= 0)
		SendMessage(hListBox,LB_DELETESTRING,i,0);
	else
		MessageBox(hListBox,L"Error: No word selected to delete.\nPlease, select a word and try again.",L"Error when delete word!",MB_ICONWARNING);
}


void MyListBox::clearList()
{
	SendMessage(hListBox,LB_RESETCONTENT,0,0);
}


int MyListBox::getSelected()
{
	int i = 0;
	i = SendMessage(hListBox,LB_GETCURSEL,0,0);
	if(i == LB_ERR)
		return -1;
	else
		return i;
}

LPTSTR MyListBox::getText(int index)
{
	TCHAR buffer[21];
	SendMessage(hListBox,LB_GETTEXT,index,(LPARAM)buffer);
	return buffer;
}
LPTSTR MyListBox::getTextSelected()
{
	LPTSTR str = new WCHAR[21];
	SendMessage(hListBox,LB_GETTEXT,SendMessage(hListBox,LB_GETCURSEL,0,0),(LPARAM)str);
	
	return str;
}

BOOL MyListBox::searchList(LPTSTR itemSearch)
{
	

	int countList;
	countList = SendMessage(hListBox,LB_GETCOUNT,0,0);
	if(!countList)
		return FALSE;

	int index = SendMessage(hListBox,LB_SELECTSTRING,-1,(LPARAM)itemSearch);
	if(index>=0)
	{
		SendMessage(hListBox,LB_SETSEL,TRUE,(LPARAM)index);
		return TRUE;
	}
	return FALSE;
}

